import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Index from '@/views/layouts/Index.vue'
import { isLogin } from '@/utils'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/login',
    name: 'home',
    component: Index,
    children: [
      { path: '1-1', component: () => import('../components/1-1.vue') },
      { path: '1-2', component: () => import('../components/1-2.vue') },
      { path: '1-3', component: () => import('../components/1-3.vue') },
      { path: '1-4-1', component: () => import('../components/1-4-1.vue') },
      { path: '2', component: () => import('../components/2.vue') },
      { path: '3', component: () => import('../components/3.vue') },
      { path: '4', component: () => import('../components/4.vue') },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    return next()
  }
  let res = isLogin()
  next(res)
})

export default router
